﻿package beautifulWorld2D.engine {
	
	import beautifulWorld2D.engine.interfaces.IPerso;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.getTimer;
	import beautifulWorld2D.engine.Engine;
	
	public class Perso extends DecorElement implements IPerso{
		
		private var _clipPerso:MovieClip;
		private var _clipPersoDroite:MovieClip;
		private var _clipPersoGauche:MovieClip;
		private var _clipPersoTest:MovieClip;
		private var _boxCollision:MovieClip;
		private var _mcLens:MovieClip;
		
		/// Etat Perso
		private var _stand:Boolean; 							/// perso en stand by
		private var _run:Boolean; 								/// perso en mouvement
		private var _pRight:Boolean; 							/// perso vue de droite
		private var _pLeft:Boolean; 							/// perso vue de gauche
		private var _jump:Boolean;								/// perso en train de sauter
		private var _jumpReception:Boolean;					 	/// perso en reception de saut
		private var _fall:Boolean;								/// perso en train de tomber
		private var _collision:Boolean;							/// perso en collison avec un objet
		private var _lens:Boolean;								/// lumiere du perso
		
						
		
		///// Parametres des animations
		private var _framerate:Number;							///// image/s des animations
		private var _gravite:Number = 0.5;						///// vitesse de chute
		private var _acceleration:Number = _gravite;				///// acceleration de la chute
		private var _TabAnimLabel:Array;
		private var _TabAnimFirstFrame:Array;
		private var _TabAnimLastFrame:Array;
		private var _lastXpos:Number;
		private var _anim_x:Number;
		private var _anim_y:Number;
		private var _startTime:Number = 0;			      			///// temps de départ de l'animation en cours
		private var _startTime2:Number = 0;			      			///// temps de départ de l'animation en cours
		private var _startTime3:Number = 0;			      			///// temps de départ de l'animation en cours	
		private var _currentFrame:Number;			      		///// frame de courante de l'animation en cours
		private var _prevFrame:Number = -1;
		private var _runActivate:Boolean = true;
		
		private var _xElementBox:int;
		private var _yElementBox:int;
		
		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function Perso(clipPerso:MovieClip, boxCollision:MovieClip,mcLens:MovieClip, pRight:Boolean){
			
			
			super();
			
			_framerate = Engine.timerRepeat;

			this._clipPerso	= clipPerso;
			this._clipPersoDroite = this._clipPerso.droite;
			this._clipPersoGauche = this._clipPerso.gauche;
			this._clipPersoTest = this._clipPerso.test;
			this._boxCollision = boxCollision;
			trace("this._boxCollision " + this._boxCollision.y);
			this._clipPersoDroite.visible = pRight;
			this._clipPersoGauche.visible = !pRight;
			this._stand = false;
			this._run	= false;
			this._pRight = pRight;
			this._pLeft	= !pRight;
			this._jump	= false;
			this._jumpReception = false;
			this._fall = false;
			this._run	= false;
			this._collision = false;
			this._lens = false;
			this._mcLens = mcLens;
			this._mcLens.visible = _lens;
			
			this._TabAnimLabel = new Array();
			this._TabAnimFirstFrame = new Array();
			this._TabAnimLastFrame = new Array();
			
			this._anim_x = this._clipPersoDroite.getChildByName("perso" + 1).x;
			this._anim_y = this._clipPersoDroite.getChildByName("perso" + 1).y;
			
			
			_xDecor = _clipPerso.x;
			_yDecor = _clipPerso.y;
			
			_xElement = _clipPerso.x;
			_yElement = _clipPerso.y;
			
			_xElementBox = _clipPerso.x;
			_yElementBox = _clipPerso.y;
			
			initPersoState(_clipPersoDroite);
			initPersoState(_clipPersoGauche);
			initPersoState(_clipPersoTest);
			
			animated = true;
			
			

		}
		public function initPersoState(clip:MovieClip):void
		{
			for (var i:Number = 0; i < clip.numChildren; i++)
			{
				clip.getChildAt(i).visible = false;
			}
			
			
		}
		


		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		
		
		
		override public function get scaleDecor():Number { return _scaleDecor; }
		
		override public function set scaleDecor(value:Number):void 
		{
			_scaleDecor = value;
			_clipPerso.scaleX = _scaleDecor * _scaleThis;
			_boxCollision.scaleX = _clipPerso.scaleX;
			_clipPerso.scaleY = _clipPerso.scaleX;
			_boxCollision.scaleY = _clipPerso.scaleX;
		}
		
		
		
		override public function get x():Number { return _xElement; }
		
		override public function set x(value:Number):void 
		{
			_xElement = value;
			xBox = value;
			xStage = _xDecor + _xElement*scaleX;
		}
		override public function get y():Number { return _yElement; }
		
		override public function set y(value:Number):void 
		{
			_yElement = value;
			yBox = value;
			yStage = _yDecor + _yElement*scaleY;
		}
		public function get xBox():Number { return _xElementBox; }
		
		public function set xBox(value:Number):void 
		{
			_xElementBox = value;
			xStageBox = _yDecor + _xElementBox*scaleX;
		}
		public function get yBox():Number { return _yElementBox; }
		
		public function set yBox(value:Number):void 
		{
			_yElementBox = value;
			yStageBox = _yDecor + _yElementBox*scaleY;
		}
		override public function get xDecor():int { return _xDecor; }
		
		override public function set xDecor(value:int):void 
		{
			_xDecor = value;
			xStage = _xDecor + _xElement*_clipPerso.scaleX;
			xStageBox = _xDecor + _xElementBox*_clipPerso.scaleX;
		}
		
		override public function get yDecor():int { return _yDecor; }
		
		override public function set yDecor(value:int):void 
		{
			_yDecor = value;
			yStage = _yDecor + _yElement*_clipPerso.scaleY;
			yStageBox = _yDecor + _yElementBox*_clipPerso.scaleY;
		}
		
		
		override public function get xStage():int { return _clipPerso.x; }
		
		override public function set xStage(value:int):void 
		{
			_clipPerso.x = value;
			_boxCollision.x = _clipPerso.x;
		}
		
		override public function get yStage():int { return _clipPerso.y; }
		
		override public function set yStage(value:int):void 
		{
			_clipPerso.y = value;
			_boxCollision.y = _clipPerso.y;
		}
		
		public function get xStageBox():int { return _boxCollision.x; }
		
		public function set xStageBox(value:int):void 
		{
			_boxCollision.x = value;
		}
		
		public function get yStageBox():int { return _boxCollision.y; }
		
		public function set yStageBox(value:int):void 
		{
			_boxCollision.y = value;
		}
		
		/*public function set x(value:Number){
			_boxCollision.x = value;
			_clipPerso.x = value;
		}
		public function set y(value:Number){
			_boxCollision.y = value;
			_clipPerso.y = value;
		}
		public function get x():Number{
			return  _clipPerso.x;
		}
		public function get y():Number{
			return _clipPerso.y;
		}*/
		
		public function get box():MovieClip{
			return this._boxCollision;
		}
		public function get clip():MovieClip{
			return this._clipPerso;
		}
		public function get isStand():Boolean{
			return this._stand;
		}
		public function set isStand(valeur:Boolean):void{
			this._stand = valeur;
			if(valeur)
			{
				this._currentFrame = _TabAnimFirstFrame[0];
				this.isRun = false;
				this.isJump = false;
				this.isFall = false;
				this.isReception = false;
			}
		}
		public function get isRight():Boolean{
			return this._pRight;
		}
		public function set isRight(valeur:Boolean){
			this._pRight = valeur;
			this._clipPersoDroite.visible = valeur;
			this._clipPersoGauche.visible = !valeur;
		}
		public function get isRun():Boolean{
			return this._run;
		}
		public function set isRun(valeur:Boolean){
			this._run = valeur;
			if(valeur){
				this._currentFrame = _TabAnimFirstFrame[1];
			}
			else{
				_clipPersoDroite.gotoAndStop(1);
				_clipPersoGauche.gotoAndStop(1);
			}
		}
		public function get isJump():Boolean{
			return this._jump;
		}
		public function set isJump(valeur:Boolean){
			this._jump = valeur;
			if(valeur){
				this._currentFrame = _TabAnimFirstFrame[2];
			}else{
				_clipPersoDroite.gotoAndStop(1);
				_clipPersoGauche.gotoAndStop(1);
			}
		}
		public function get isFall():Boolean{
			return this._fall;
		}
		public function set isFall(valeur:Boolean){
			this._fall = valeur;

		}
		public function get isReception():Boolean{
			return this._jumpReception;
		}
		public function set isReception(valeur:Boolean){
			this._jumpReception = valeur;
			this._currentFrame = _TabAnimFirstFrame[3];
		}
		public function get isCollision():Boolean{
			return this._collision;
		}
		public function set isCollision(valeur:Boolean){
			this._collision = valeur;
		}
		public function get startTime():Number{
			return this._startTime;
		}
		public function set startTime(valeur:Number){
			this._startTime = valeur;
		}
		public function set lens(valeur:Boolean){
			this._lens = valeur;
			this._mcLens.visible = valeur;
		}
		public function get lens():Boolean{
			return this._lens;
		}
		
		public function get runActivate():Boolean { return _runActivate; }
		
		public function set runActivate(value:Boolean):void 
		{
			_runActivate = value;
		}
		
		public function get startTime2():Number { return _startTime2; }
		
		public function set startTime2(value:Number):void 
		{
			_startTime2 = value;
		}
		
		public function get startTime3():Number { return _startTime3; }
		
		public function set startTime3(value:Number):void 
		{
			_startTime3 = value;
		}
		
		public function get acceleration():Number { return _acceleration; }
		
		public function set acceleration(value:Number):void 
		{
			_acceleration = value;
		}
		
		

		

		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function setBoxPos(){
			x = xBox;
			y = yBox;
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function setLensPos()
		{
			this._mcLens.x = xStage;
			this._mcLens.y = yStage;
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function setBoxPosX(){
			x = xBox;
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function setBoxPosY(){
			y = yBox;
		}

		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function initAcceleration(){
				_acceleration = _gravite;
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function initAcceleration2(){
				_acceleration = 10;
		}

		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function initAnimStand(firstFrame:Number, lastFrame:Number):void{
			this._TabAnimLabel[0] = new String("stand");
			
			this._TabAnimFirstFrame[0] = new Number();
			this._TabAnimFirstFrame[0] = firstFrame;
			
			this._TabAnimLastFrame[0] = new Number();
			this._TabAnimLastFrame[0] = lastFrame;

		}
		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function initAnimRun(firstFrame:Number, lastFrame:Number):void{
			
			this._TabAnimLabel[1] = new String("run");

			this._TabAnimFirstFrame[1] = new Number();
			this._TabAnimFirstFrame[1] = firstFrame;
			
			this._TabAnimLastFrame[1] = new Number();
			this._TabAnimLastFrame[1] = lastFrame;

		}
		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function initAnimJump(firstFrame:Number, lastFrame:Number):void{
			
			this._TabAnimLabel[2] = new String("jump");
			
			this._TabAnimFirstFrame[2] = new Number();
			this._TabAnimFirstFrame[2] = firstFrame;
			
			this._TabAnimLastFrame[2] = new Number();
			this._TabAnimLastFrame[2] = lastFrame;

		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function initAnimReception(firstFrame:Number, lastFrame:Number):void{
			
			this._TabAnimLabel[3] = new String("reception");
			
			this._TabAnimFirstFrame[3] = new Number();
			this._TabAnimFirstFrame[3] = firstFrame;
			
			this._TabAnimLastFrame[3] = new Number();
			this._TabAnimLastFrame[3] = lastFrame;

		}	
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function fall(){
			var timeDiff = 1;
			if(!isNaN(_startTime3) && _startTime3 != -1){
				timeDiff = getTimer() - _startTime3;
				timeDiff = int( (timeDiff/1000) * _framerate);
			}
			
					
			yBox += _acceleration*timeDiff;
			//trace("currentFrame Fall : " + _boxCollision.y);
				if(_acceleration < 15)
					_acceleration++;
					
					
		}
		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function stand(){
			
		
				if(this._currentFrame <= _TabAnimLastFrame[0]){
					_clipPersoDroite.gotoAndStop(this._currentFrame);
					_clipPersoGauche.gotoAndStop(this._currentFrame);
					this._currentFrame++;
				}else{
					_clipPersoDroite.gotoAndStop(_TabAnimFirstFrame[0]);
					_clipPersoGauche.gotoAndStop(_TabAnimFirstFrame[0]);
					this._currentFrame = _TabAnimFirstFrame[0];
				}
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function standTimer(){
			
			var timeDiff = getTimer() - _startTime;
			timeDiff = int( (timeDiff/1000) * _framerate);
			var currentFrame = timeDiff + _TabAnimFirstFrame[0];
			//trace("currentFrame Stand : " + currentFrame);
				if(currentFrame <= _TabAnimLastFrame[0]){
					//_clipPersoDroite.gotoAndStop(currentFrame);
					//_clipPersoGauche.gotoAndStop(currentFrame);
					_currentFrame = currentFrame;
		
				}else{
						//_clipPersoDroite.gotoAndStop(_TabAnimFirstFrame[0]);
						//_clipPersoGauche.gotoAndStop(_TabAnimFirstFrame[0]);
						_currentFrame = _TabAnimFirstFrame[0];
						_startTime = getTimer();
				}
			setAnim();
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function initFrameRun(){
			this._currentFrame = _TabAnimFirstFrame[1];
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function run(){
			

			var sens:Number;
			var ajout:Number;
			
			if (this._pRight)
				sens = 1;
			else
				sens = -1;
				
			//trace("Run");

				if(this._currentFrame <= _TabAnimLastFrame[1]){
					//trace("currentFrame Run : " + this._currentFrame);
					_clipPersoDroite.gotoAndStop(this._currentFrame);
					_clipPersoGauche.gotoAndStop(this._currentFrame);
					
					_lastXpos = _boxCollision.x;
					ajout = (_clipPersoDroite.perso.x - _anim_x);
					_boxCollision.x = _clipPerso.x + sens*ajout;
					this._currentFrame++;
				}else{
					_clipPersoDroite.gotoAndStop(_TabAnimFirstFrame[1]);
					_clipPersoGauche.gotoAndStop(_TabAnimFirstFrame[1]);
					_lastXpos = _clipPerso.x = _boxCollision.x;
					this._currentFrame = _TabAnimFirstFrame[1];
				}
		}
		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function runTimer(){
			
			if ( !_runActivate) {
				return;
			}
			var sens:Number;
			var ajout:Number;
			
			if (this._pRight)
				sens = 1;
			else
				sens = -1;
				
			//trace("Run");
			var timeDiff = getTimer() - _startTime;
			timeDiff = int( (timeDiff/1000) * _framerate);
			var currentFrame = timeDiff + _TabAnimFirstFrame[1];
			//trace("currentFrame Run : " + currentFrame);
				if(currentFrame <= _TabAnimLastFrame[1]){
					//trace("currentFrame Run : " + this._currentFrame);
					//_clipPersoTest.gotoAndStop(currentFrame);
					
					var box:DisplayObject = _clipPersoTest.getChildByName("perso" + currentFrame);
					_lastXpos = _xElementBox;
					ajout = (box.x - _anim_x);
					xBox = _xElement + sens*ajout;
					

				}else{
					//_clipPersoTest.gotoAndStop(_TabAnimFirstFrame[1]);
					_lastXpos = x = xBox;
					currentFrame = _TabAnimFirstFrame[1];
					_startTime = getTimer();
					setBoxPosX();
				}
			this._currentFrame = currentFrame;
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function setAnim(){
			
			//_clipPersoDroite.gotoAndStop(this._currentFrame);
			//_clipPersoGauche.gotoAndStop(this._currentFrame);
			
			
			if (_prevFrame != -1)
			{
				_clipPersoDroite.getChildByName("perso" + _prevFrame).visible = false;
				_clipPersoGauche.getChildByName("perso" + _prevFrame).visible = false;
			}
			_prevFrame = _currentFrame;
			_clipPersoDroite.getChildByName("perso" + _currentFrame).visible = true;
			_clipPersoGauche.getChildByName("perso" + _currentFrame).visible = true;
			
			
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function jump():Number{
			var etat = 0;
			
			//trace("Jump");
			//trace("currentFrame Jump : " + this._currentFrame);
				if(this._currentFrame <= _TabAnimLastFrame[2]){
					_clipPersoDroite.gotoAndStop(this._currentFrame);
					_clipPersoGauche.gotoAndStop(this._currentFrame);
						
					_boxCollision.y = _clipPerso.y - (_anim_y - _clipPersoDroite.perso.y);
					this._currentFrame++;
				}else{
					_clipPersoDroite.gotoAndStop(_TabAnimFirstFrame[2]);
					_clipPersoGauche.gotoAndStop(_TabAnimFirstFrame[2]);	
					this._jump = false;
					this._currentFrame = _TabAnimFirstFrame[2];
				}
				
				if(this._currentFrame >= 95){
						etat = 1;
				}
			return etat;
		}
		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function jumpTimer():Number{
			var etat = 0;
			
			var timeDiff = getTimer() - _startTime;
			timeDiff = int( (timeDiff/1000) * _framerate);
			var currentFrame = timeDiff + _TabAnimFirstFrame[2];
			
				if (currentFrame <= _TabAnimLastFrame[2])
				{
					//_clipPersoTest.gotoAndStop(currentFrame);
					
					var box:DisplayObject = _clipPersoTest.getChildByName("perso" + currentFrame);
					yBox = y - (_anim_y - box.y);

				}else{
					//_clipPersoTest.gotoAndStop(_TabAnimLastFrame[2]);
					currentFrame = _TabAnimFirstFrame[2];
					_startTime = getTimer();
					etat = 2;
				}
				
				if(currentFrame >= 95 && etat != 2){
						etat = 1;
				}
			this._currentFrame = currentFrame;
				
			return etat;
		}
		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function reception(){
			
			//trace("Reception");
			//trace("currentFrame Reception : " + this._currentFrame);
				if(this._currentFrame <= _TabAnimLastFrame[3]){
					_clipPersoDroite.gotoAndStop(this._currentFrame);
					_clipPersoGauche.gotoAndStop(this._currentFrame);		
					this._currentFrame++;
				}else{
					this.isReception = false;
					this.isJump = false;
					if(this._run) initFrameRun();
					this._currentFrame = _TabAnimFirstFrame[3];
					_startTime = getTimer();
				}
		}
		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function receptionTimer(){
			
			//trace("Reception");
			var timeDiff = getTimer() - _startTime;
			timeDiff = int( (timeDiff/1000) * _framerate);
			var currentFrame = timeDiff + _TabAnimFirstFrame[3];
			
				if(currentFrame <= _TabAnimLastFrame[3]){
					//_clipPersoDroite.gotoAndStop(currentFrame);
					//_clipPersoGauche.gotoAndStop(currentFrame);
					_currentFrame = currentFrame;
					setAnim();
				}else{
					this.isReception = false;
					if(this._run) initFrameRun();
					currentFrame = _TabAnimFirstFrame[3];
					this._startTime = getTimer();
				}
				

		}
		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function translateX(){
			var sens:Number;
			if (this._pRight)
				sens = 1;
			else
				sens = -1;
			
			var timeDiff = 1;
			if(!isNaN(_startTime2) && _startTime2 != -1){
				timeDiff = getTimer() - _startTime2;
				timeDiff = int( (timeDiff/1000) * _framerate);
			}
			
			
			_lastXpos = xBox;
			xBox += sens*10*timeDiff;
		}
		
		
		
	}
}